<?php
if (!defined('APPPATH')) exit('No direct script access allowed');
class user2usergroup_db extends Model
{
	public $table = 'ptx_user_usergroup';
	public $pk = 'user_id';
	
	var $linker = array(
			'user'=>array(
					'type' => 'hasone',
					'map' => 'user',
					'mapkey' => 'user_id',
					'ftable' => 'ptx_user',
					'fkey' => 'user_id',
					'enabled' => false
			),
			'usergroup'=>array(
					'type' => 'hasone',
					'map' => 'usergroup',
					'mapkey' => 'usergroup_id',
					'ftable' => 'ptx_usergroup',
					'fkey' => 'usergroup_id',
					'enabled' => false
			),
		);
	var $select_fields =
	" ptx_user_usergroup.user_id,ptx_user_usergroup.usergroup_id,user.user_title,user.email,user.nickname,user.create_time,
			usergroup.usergroup_name,usergroup.usergroup_key,usergroup.usergroup_type ";

	var $sort = " ptx_user_usergroup.user_id ASC ";
	
	public function getUsersByGroup($groupid,$page, $pagesize){
		$this->linker['user']['enabled'] = true;
		$this->linker['usergroup']['enabled'] = true;
		$sort = " ptx_user_usergroup.user_id ASC ";
		$condition['usergroup_id'] = $groupid;
		return $this->pager($page, $pagesize)->findAllJoin($condition,$sort,$this->select_fields);
	}
	
	public function getGroupsByUser($uid,$field=' ptx_user_usergroup.usergroup_id,usergroup.usergroup_type '){
		$sort = " ptx_user_usergroup.usergroup_id ASC ";
		$this->linker['usergroup']['enabled'] = true;
		$condition['user_id'] = $uid;
		return $this->findAllJoin($condition, $sort, $field);
	}
	
	public function getUserGroupByType($uid,$type){
		$condition = " ptx_user_usergroup.user_id='$uid' AND usergroup.usergroup_type='$type' ";
		$this->enableLinker('usergroup');
		return $this->findAllJoin($condition,$this->sort);
	}
	
	public function getGroupsDetailByUser($uid,$field=' ptx_user_usergroup.usergroup_id,usergroup.usergroup_name '){
		$this->enableLinker('usergroup');
		$sort = " ptx_user_usergroup.usergroup_id ASC ";
		$condition['user_id'] = $uid;
		return $this->findAllJoin($condition, $sort, $field);
	}
	
	
	public function get_value($key){
		$settings = $this->getSettings();
		return unserialize($settings[$key]);
	}
	
	public function getGroupCache($key){
		$result = pcache('r', 'group_'.$key);
		if(!$result){
			return $this->updateSettings($key);
		}
		return $result;
	}
	
	public function updateGroupCache($key){
		$condition['usergroup_type'] = $key;
		if($sysGroups = $this->findAll($condition)){
			$newGs = array();
			foreach ($sysGroups as $g) {
				if($g['other_permission']){
					$op = unserialize($g['other_permission']);
					foreach ($op as $key=>$v){
						$g[$key] = $v;
					}
				}
				$newGs[$g['usergroup_id']] = $g;
			}
			unset($sysGroups);
			pcache('w','group_'.$key,$newGs);
			return $newGs;
		}
		return false;
	}
	
	public function updateUserGroup($uid,$gid,$type){
		$gs = $this->getUserGroupByType($uid,$type);
		foreach ($gs as $g){
			$this->delete(array('usergroup_id'=>$g['usergroup_id'],'user_id'=>$uid));
		}
		$this->add_one(array('usergroup_id'=>$gid,'user_id'=>$uid));
	}
	
	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			$result = $this->create($data);
			return $result;
		}
		return false;
	}
	
	private function check_value($data){
		if($data['usergroup_id']==null||$data['user_id']==null){
			return false;
		}
		return true;
	}
	
	public function get_round($category_id,$limit = null){
		$dbpre = $this->dbpre;
		if($category_id)
			$where = " ptx_share.category_id = {$category_id} AND item.is_deleted=0 ORDER BY ptx_share.share_id ";
		else
			$where = " item.is_deleted=0 ORDER BY ptx_share.share_id ";
		$sql = " SELECT {$this->select_fields}
		FROM  `{$dbpre}ptx_share` AS ptx_share
		JOIN (
		SELECT ROUND(
		RAND() *
		((SELECT MAX( share_id ) FROM  `{$dbpre}ptx_share` ) - ( SELECT MIN( share_id ) FROM  `{$dbpre}ptx_share` ))
		+ ( SELECT MIN( share_id ) FROM  `{$dbpre}ptx_share` )
		) AS share_rand_id
		) AS t2
		ON ptx_share.share_id >= t2.share_rand_id
		LEFT JOIN  `{$dbpre}ptx_item` item ON ptx_share.item_id = item.item_id
		LEFT JOIN  `{$dbpre}ptx_item_detail` detail ON ptx_share.item_id = detail.item_id
		LEFT JOIN  `{$dbpre}ptx_category` category ON ptx_share.category_id = category.category_id
		LEFT JOIN  `{$dbpre}ptx_album` album ON ptx_share.album_id = album.album_id
		WHERE {$where}";
	
		($limit) ? $sql.= " LIMIT {$limit}" : '';
	
		$result = $this->findSql($sql);
		return $result;
	
		}
	
	
	public function searchUser($keyword,$sysid,$memid){
		$dbpre = $this->dbpre;
		$conditions_user = null;
		if($sysid){
			$conditions_user .= " AND u.user_id IN (select ug.user_id from `{$dbpre}ptx_user_usergroup` ug where ug.usergroup_id='{$sysid}') ";
		}
		if($memid){
			$conditions_user .= " AND u.user_id IN (select ug.user_id from `{$dbpre}ptx_user_usergroup` ug where ug.usergroup_id='{$memid}') ";
		}
		
		if(isset($keyword)){
			$conditions_user .= " AND u.nickname LIKE '%{$keyword}%' OR u.email LIKE '%{$keyword}%' ";
		}
		if(strpos($conditions_user, 'AND') === 0){
			$conditions_user = substr($conditions_user, 3);
		}
		
		if($conditions_user){
			$sql = "SELECT * from ptx_user u WHERE $conditions_user";
		}else{
			$sql = "SELECT * from ptx_user u";
		}
		
	}

}
